<?php
/**
 * @category    Inchoo
 * @package     Inchoo_Facebook
 * @author      Ivan Weiler <ivan.weiler@gmail.com>
 * @copyright   Inchoo (http://inchoo.net)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
?>
<!-- BEGIN Inchoo Facebook Connect -->
<div id="fb-root"></div>
<script type="text/javascript">
//<![CDATA[
window.fbAsyncInit = function() {
	FB.init({
		appId		: <?php echo json_encode($this->getApiKey()) ?>,
		channelUrl	: '<?php echo $this->getChannelUrl() ?>',
		status		: true,
		cookie		: true,
		oauth		: true,
		xfbml		: true
	});
	document.fire("facebook:fbAsyncInit");
};

(function(d){
	var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
	js = d.createElement('script'); js.id = id; js.async = true;
	js.src = "//connect.facebook.net/<?php echo $this->getLocale() ?>/all.js";
	d.getElementsByTagName('head')[0].appendChild(js);
}(document));

document.observe('click', function(e){
	var target = e.findElement('a[rel^=facebook-connect]') || e.findElement('button[rel^=facebook-connect]');
	if (target && target.readAttribute('rel')=='facebook-connect') {
		e.stop();
		try{
			FB.login(function(response){
				if(response.status=='connected') setLocation('<?php echo $this->getConnectUrl() ?>');
			}, {scope: <?php echo $this->getRequiredPermissions() ?>});
		}catch(error){}
	}
});
//]]>
</script>
<!-- END Inchoo Facebook Connect -->
